package cn.topevery.garden.dal.dao.impl;

import cn.topevery.garden.dal.dao.CurrStatusCarDao;
import cn.topevery.garden.entity.po.CurrStatusCar;
import cn.topevery.garden.entity.query.CurrStatusCarQuery;
import cn.topevery.garden.entity.vo.CurrStatusCarVO;
import cn.topevery.garden.framework.base.BaseDaoImpl;
import hw.topevery.framework.db.CommandType;
import hw.topevery.framework.db.entity.DataPager;
import hw.topevery.framework.db.entity.SqlQueryMate;
import hw.topevery.framework.db.enums.ScriptConditionEnum;
import hw.topevery.framework.entity.DbCsp;
import hw.topevery.framework.entity.Value;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Repository;

import java.util.List;

/**
 * 
 *
 * @author code-generate
 * @date 2021-09-01
 */
@Repository
public class CurrStatusCarDaoImpl extends BaseDaoImpl<CurrStatusCar> implements CurrStatusCarDao {
    @Override
    public <P extends DbCsp> List<CurrStatusCar> getSearch(P para) {
        return null;
    }

    

    @Override
    public DataPager<CurrStatusCarVO> listPage(CurrStatusCarQuery query) {
        String table = "t_curr_status_car tcsc " +
                "join t_car tc on tcsc.c_number = tc.c_car_num "
                ;


        String columns = "tcsc.*";
        int pageIndex = query.pageIndex - 1;
        int pageSize = query.pageSize;

        SqlQueryMate sqlQueryMate = new SqlQueryMate(table).setColumns(columns).setOrderBy("tcsc.c_id desc");
        sqlQueryMate
                .where(true, "tcsc.c_db_status", ScriptConditionEnum.Eq, 0)
                .where(true, "tc.c_db_status", ScriptConditionEnum.Eq, 0)
                //.where(true, "dr.c_object_type", ScriptConditionEnum.Eq, 1)
                .where(StringUtils.isNotBlank(query.getNumber()), "tcsc.c_number", ScriptConditionEnum.Like, query.getNumber())
                //.where(StringUtils.isNotBlank(query.getDeviceCode()), "dr.c_code", ScriptConditionEnum.Like, query.getDeviceCode())
                .where(query.getExtentId() != null, "tc.c_extent_id", ScriptConditionEnum.Eq, query.getExtentId())
                .where(query.getOnlineType() != null, "tcsc.c_online_type", ScriptConditionEnum.Eq, query.getOnlineType())
                .where(query.getOnguardType() != null, "tcsc.c_onguard_type", ScriptConditionEnum.Eq, query.getOnguardType())
                .where(StringUtils.isNotBlank(query.getSectionId()),"tcsc.c_section_id",ScriptConditionEnum.Eq,query.getSectionId())
        ;

        return getDataPager(sqlQueryMate, pageSize, pageIndex, CurrStatusCarVO.class);
    }

    public Boolean uptOnlineStatus(){
        Value<Boolean> res = new Value<>();
        String sql = "UPDATE t_curr_status_car SET c_online_type=0\n" +
                "where c_db_status=0\n" +
                "and c_online_type=1\n" +
                "and c_gps_date<DATE_ADD(NOW(), INTERVAL -30 MINUTE)";
        run(sql, CommandType.Text, dbCmd -> {
            res.data = dbCmd.executeNonQuery();
        });
        return res.data;
    }
}
